/*
 * Ant Group
 * Copyright (c) 2004-2024 All Rights Reserved.
 */
package com.alipay.zmdepositdemo.service.trade;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.AlipayDataDataserviceBillDownloadurlQueryModel;
import com.alipay.api.domain.AlipayTradeCloseModel;
import com.alipay.api.domain.AlipayTradeFastpayRefundQueryModel;
import com.alipay.api.domain.AlipayTradePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayDataDataserviceBillDownloadurlQueryRequest;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayDataDataserviceBillDownloadurlQueryResponse;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.alipay.api.response.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.api.response.AlipayTradePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.alipay.zmdepositdemo.model.OpenResponse;
import com.alipay.zmdepositdemo.model.TradeDeductModel;
import com.alipay.zmdepositdemo.model.TradeRefundModel;
import com.alipay.zmdepositdemo.util.AlipayApiCallback;
import com.alipay.zmdepositdemo.util.AlipayApiTemplate;
import com.alipay.zmdepositdemo.util.AlipaySdkUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhangjian
 * @version DepositOrderTradeService.java, v 0.1 2024年06月17日 15:39 zhangjian
 */
@Service
public class TradeOrderServiceImpl implements TradeOrderService {

    private static final Logger logger = LogManager.getLogger(TradeOrderServiceImpl.class);

    private AlipaySdkUtil alipaySdkUtil;

    @Autowired
    public void ConstructorAlipaySdkUtil(AlipaySdkUtil alipaySdkUtil) {
        this.alipaySdkUtil = alipaySdkUtil;
    }

    /**
     * 发起扣款
     *
     * @return AlipayTradePayResponse
     */
    @Override
    public OpenResponse<AlipayTradePayResponse> tradePay(TradeDeductModel tradeDeductModel) {
        return AlipayApiTemplate.execute(new AlipayApiCallback<AlipayTradePayResponse, AlipayTradePayResponse>() {
            @Override
            public AlipayTradePayResponse process() throws AlipayApiException {
                AlipayTradePayModel model = new AlipayTradePayModel();

                model.setOutTradeNo(tradeDeductModel.getOutTradeNo());
                model.setTotalAmount(tradeDeductModel.getTotalAmount());
                model.setSubject(tradeDeductModel.getSubject());
                model.setProductCode(tradeDeductModel.getProductCode());
                model.setAuthNo(tradeDeductModel.getAuthNo());

                model.setAuthConfirmMode(tradeDeductModel.getAuthConfirmMode());
                model.setGoodsDetail(tradeDeductModel.getGoodsDetail());
                model.setExtendParams(tradeDeductModel.getExtendParams());
                model.setPromoParams(tradeDeductModel.getPromoParams());
                model.setStoreId(tradeDeductModel.getStoreId());

                model.setTerminalId(tradeDeductModel.getTerminalId());
                model.setQueryOptions(tradeDeductModel.getQueryOptions());

                AlipayTradePayRequest request = alipayTradePayRequestBuilder(model);
                logger.info("com.alipay.zmdepositdemo.service.trade.DepositOrderTradeServiceImpl.tradePay,AlipayTradePayRequest:" +
                        JSON.toJSONString(request));
                return alipaySdkUtil.execute(request);
            }

            @Override
            public AlipayTradePayResponse getData(AlipayTradePayResponse response) {
                return response;
            }

            @Override
            public String getApiName() {
                return "发起扣款-alipay.trade.pay(统一收单交易支付接口)";
            }
        });
    }

    private AlipayTradePayRequest alipayTradePayRequestBuilder(AlipayTradePayModel model) {
        AlipayTradePayRequest request = new AlipayTradePayRequest();

        request.setBizModel(model);
        return request;
    }

    /**
     * 查询扣款
     *
     * @return 返回结果
     */
    @Override
    public OpenResponse<AlipayTradeQueryResponse> queryPay(String outTradeNo,
                                                           String tradeNo,
                                                           String orgPid,
                                                           List<String> queryOptions) {
        return AlipayApiTemplate.execute(new AlipayApiCallback<AlipayTradeQueryResponse, AlipayTradeQueryResponse>() {
            @Override
            public AlipayTradeQueryResponse process() throws AlipayApiException {
                AlipayTradeQueryModel model = new AlipayTradeQueryModel();
                model.setOutTradeNo(outTradeNo);
                model.setTradeNo(tradeNo);
                model.setOrgPid(orgPid);
                model.setQueryOptions(queryOptions);

                AlipayTradeQueryRequest request = alipayTradeQueryRequestBuilder(model);
                logger.info("com.alipay.zmdepositdemo.service.trade.DepositOrderTradeServiceImpl.queryPay,AlipayTradeQueryRequest:" +
                        JSON.toJSONString(request));
                return alipaySdkUtil.execute(request);
            }

            @Override
            public AlipayTradeQueryResponse getData(AlipayTradeQueryResponse response) {
                return response;
            }

            @Override
            public String getApiName() {
                return "查询扣款-alipay.trade.query(统一收单交易查询)";
            }
        });
    }

    private AlipayTradeQueryRequest alipayTradeQueryRequestBuilder(AlipayTradeQueryModel model) {
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();

        request.setBizModel(model);
        return request;
    }

    /**
     * 取消扣款
     *
     * @return boolean true 取消扣款成功
     */
    @Override
    public OpenResponse<Boolean> cancelPay(String outTradeNo,
                                           String tradeNo,
                                           String operatorId) {
        return AlipayApiTemplate.execute(new AlipayApiCallback<Boolean, AlipayTradeCloseResponse>() {
            @Override
            public AlipayTradeCloseResponse process() throws AlipayApiException {
                AlipayTradeCloseModel model = new AlipayTradeCloseModel();
                model.setOutTradeNo(outTradeNo);
                model.setTradeNo(tradeNo);
                model.setOperatorId(operatorId);

                AlipayTradeCloseRequest request = alipayTradeCloseRequestBuilder(model);
                logger.info("com.alipay.zmdepositdemo.service.trade.DepositOrderTradeServiceImpl.cancelPay,AlipayTradeCloseRequest:" +
                        JSON.toJSONString(request));

                return alipaySdkUtil.execute(request);
            }

            @Override
            public Boolean getData(AlipayTradeCloseResponse response) {
                return response.isSuccess();
            }

            @Override
            public String getApiName() {
                return "取消扣款-alipay.trade.close(统一收单交易关闭接口)";
            }
        });
    }

    private AlipayTradeCloseRequest alipayTradeCloseRequestBuilder(AlipayTradeCloseModel model) {
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
        request.setBizModel(model);

        return request;
    }

    /**
     * 申请退款
     *
     * @return 返回结果
     */
    @Override
    public OpenResponse<Boolean> tradeRefund(TradeRefundModel tradeRefundModel) {
        return AlipayApiTemplate.execute(new AlipayApiCallback<Boolean, AlipayTradeRefundResponse>() {
            @Override
            public AlipayTradeRefundResponse process() throws AlipayApiException {
                AlipayTradeRefundModel model = new AlipayTradeRefundModel();
                model.setRefundAmount(tradeRefundModel.getRefundAmount());
                model.setOutTradeNo(tradeRefundModel.getOutTradeNo());
                model.setTradeNo(tradeRefundModel.getTradeNo());
                model.setRefundReason(tradeRefundModel.getRefundReason());
                model.setOutTradeNo(tradeRefundModel.getOutTradeNo());
                model.setRefundGoodsDetail(tradeRefundModel.getRefundGoodsDetail());
                model.setRefundRoyaltyParameters(tradeRefundModel.getRefundRoyaltyParameters());
                model.setQueryOptions(tradeRefundModel.getQueryOptions());
                model.setRefundAmount(tradeRefundModel.getRefundAmount());

                AlipayTradeRefundRequest request = alipayTradeRefundRequestBuilder(model);
                logger.info("com.alipay.zmdepositdemo.service.trade.DepositOrderTradeServiceImpl.tradeRefund,AlipayTradeRefundRequest:" +
                        JSON.toJSONString(request));
                return alipaySdkUtil.execute(request);
            }

            @Override
            public Boolean getData(AlipayTradeRefundResponse response) {
                return response.isSuccess();
            }

            @Override
            public String getApiName() {
                return "申请退款-alipay.trade.refund(统一收单交易退款接口)";
            }
        });
    }

    private AlipayTradeRefundRequest alipayTradeRefundRequestBuilder(AlipayTradeRefundModel model) {
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        request.setBizModel(model);

        return request;
    }

    /**
     * 查询交易退款
     *
     * @return 返回结果
     */
    @Override
    public OpenResponse<AlipayTradeFastpayRefundQueryResponse> queryRefund(String outRequestNo,
                                                                           String outTradeNo,
                                                                           String tradeNo,
                                                                           List<String> queryOptions) {
        return AlipayApiTemplate.execute(
                new AlipayApiCallback<AlipayTradeFastpayRefundQueryResponse, AlipayTradeFastpayRefundQueryResponse>() {
                    @Override
                    public AlipayTradeFastpayRefundQueryResponse process() throws AlipayApiException {
                        AlipayTradeFastpayRefundQueryModel model = new AlipayTradeFastpayRefundQueryModel();
                        model.setOutRequestNo(outRequestNo);
                        model.setOutTradeNo(outTradeNo);
                        model.setTradeNo(tradeNo);
                        model.setQueryOptions(queryOptions);
                        AlipayTradeFastpayRefundQueryRequest request = alipayTradeFastpayRefundQueryRequestBuilder(model);
                        logger.info(
                                "com.alipay.zmdepositdemo.service.trade.DepositOrderTradeServiceImpl.queryRefund,"
                                        + "AlipayTradeFastpayRefundQueryRequest:" +
                                        JSON.toJSONString(request));
                        return alipaySdkUtil.execute(request);
                    }

                    @Override
                    public AlipayTradeFastpayRefundQueryResponse getData(AlipayTradeFastpayRefundQueryResponse response) {
                        return response;
                    }

                    @Override
                    public String getApiName() {
                        return "查询交易退款-alipay.trade.fastpay.refund.query(统一收单交易退款查询)";
                    }
                });
    }

    private AlipayTradeFastpayRefundQueryRequest alipayTradeFastpayRefundQueryRequestBuilder(AlipayTradeFastpayRefundQueryModel model) {
        AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
        request.setBizModel(model);
        return request;
    }

    /**
     * 查询对账单下载地址
     *
     * @return 返回结果
     */
    @Override
    public OpenResponse<AlipayDataDataserviceBillDownloadurlQueryResponse> queryBillDownloadUrl(String billType,
                                                                                                String billDate,
                                                                                                String smId) {
        return AlipayApiTemplate.execute(
                new AlipayApiCallback<AlipayDataDataserviceBillDownloadurlQueryResponse,
                        AlipayDataDataserviceBillDownloadurlQueryResponse>() {
                    @Override
                    public AlipayDataDataserviceBillDownloadurlQueryResponse process() throws AlipayApiException {

                        AlipayDataDataserviceBillDownloadurlQueryModel model = new AlipayDataDataserviceBillDownloadurlQueryModel();

                        model.setBillType(billType);
                        model.setBillDate(billDate);
                        model.setSmid(smId);

                        AlipayDataDataserviceBillDownloadurlQueryRequest request =
                                alipayDataDataserviceBillDownloadurlQueryRequestBuilder(model);
                        logger.info(
                                "com.alipay.zmdepositdemo.service.trade.DepositOrderTradeServiceImpl.queryBillDownloadUrl,"
                                        + "AlipayDataDataserviceBillDownloadurlQueryRequest:" +
                                        JSON.toJSONString(request));
                        return alipaySdkUtil.execute(request);
                    }

                    @Override
                    public AlipayDataDataserviceBillDownloadurlQueryResponse getData(
                            AlipayDataDataserviceBillDownloadurlQueryResponse response) {
                        return response;
                    }

                    @Override
                    public String getApiName() {
                        return "查询对账单下载地址-alipay.data.dataservice.bill.downloadurl.query(查询对账单下载地址)";
                    }
                });
    }

    /**
     * 查询查询对账单下载地址入参构造
     *
     * @param model 查询模型
     * @return AlipayDataDataserviceBillDownloadurlQueryRequest
     */
    private AlipayDataDataserviceBillDownloadurlQueryRequest alipayDataDataserviceBillDownloadurlQueryRequestBuilder(
            AlipayDataDataserviceBillDownloadurlQueryModel model) {

        AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest();
        request.setBizModel(model);
        return request;

    }

}